{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 噪声模拟器\n",
    "\n",
    "MindQuantum 中包含各种噪声信道，利用噪声信道我们可以对真实的量子芯片进行模拟。下面介绍如何利用 MindQuantum 完成此任务。\n",
    "\n",
    "## ChannelAdder\n",
    "\n",
    "`ChannelAdder` 是一个能够在量子线路特定位置添加特定信道的处理器，例如在测量门后添加比特翻转信道。`ChannelAdder` 类主要由三个函数构成，`_accepter()`、`_excluder()` 和 `_handler(BasicGate)`，其功能对应如下：\n",
    "\n",
    "- `_accepter()`：返回一个由函数构成的列表，称为接受规则集，其中每个接受规则函数的输入都是一个量子门，当函数返回值为 `True` 时表示我们可以在该量子门后添加信道。\n",
    "\n",
    "- `_excluder()`：返回一个由函数构成的列表，称为拒绝规则集，其中每个拒绝规则函数的输入都是一个量子门，当函数返回值为 `True` 时表示我们拒绝在该量子门后添加信道。\n",
    "\n",
    "- `_handler(BasicGate)`：输入一个量子门，返回一段量子线路，表示在输入量子门后添加一段自定义的信道。\n",
    "\n",
    "我们从定义类 `ChannelAdder` 的 `__call__` 函数，直接调用 `ChannelAdder` 即可生成处理后的量子线路。\n",
    "下面介绍几中 `ChannelAdder`。\n",
    "\n",
    "### BitFlipAdder\n",
    "\n",
    "```python\n",
    "BitFlipAdder(flip_rate: float = None, with_ctrl=True, device: NaiveChip = None, add_after: bool = True)\n",
    "```\n",
    "\n",
    "该 `Adder` 会在所有门后（如果 `add_after` 为 `True`, 否则是在量子门前）添加一个比特翻转信道："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"216.8\" height=\"140.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"196.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RX\n",
       " </text>\n",
       "<text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "a\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f9f7b023550>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.circuit.channel_adder import BitFlipAdder\n",
    "from mindquantum.core import gates as G\n",
    "from mindquantum.core.circuit import Circuit\n",
    "\n",
    "circ = Circuit()+G.H(0)+G.RX('a').on(1)+G.Z(1, 0)\n",
    "circ.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"336.8\" height=\"140.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "BFC\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RX\n",
       " </text>\n",
       "<text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "a\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "BFC\n",
       " </text>\n",
       "\n",
       "<circle cx=\"212.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" />\n",
       "<text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "BFC\n",
       " </text>\n",
       "\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f9f781dda90>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bit_flip_adder = BitFlipAdder(0.3, with_ctrl=False)\n",
    "new_circ = bit_flip_adder(circ)\n",
    "new_circ.svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MeasureAccepter\n",
    "\n",
    "```python\n",
    "MeasureAccepter()\n",
    "```\n",
    "\n",
    "该 `Adder` 会选择那些测量门，它目前只是一个 `Accepter`，不会改变量子线路中的任何门，需要利用 `MixerAdder`，跟其他的 `Adder` 搭配使用。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MixerAdder\n",
    "\n",
    "```python\n",
    "MixerAdder(adders: typing.List[ChannelAdderBase])\n",
    "```\n",
    "\n",
    "`MixerAdder` 可以加多个 `Adder` 混合起来，保证量子门在每一个 `Adder` 中的接受函数集和拒绝函数集同时满足时，顺序添加 `_handler` 产生的量子线路。\n",
    "\n",
    "举例来说，我们可以将上文提到的 `BitFlipAdder` 和 `MeasureAccepter` 混合起来，达到只在测量门前添加比特翻转信道的功能。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MixerAdder<\n",
      "  BitFlipAdder<flip_rate=0.01, with_ctrl=True>\n",
      "  MeasureAccepter<>\n",
      ">\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"276.8\" height=\"140.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RX\n",
       " </text>\n",
       "<text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "a\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "<rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n",
       "<circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" />\n",
       "<path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n",
       "<path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" />\n",
       "</svg></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"336.8\" height=\"140.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RX\n",
       " </text>\n",
       "<text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "a\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"192.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" />\n",
       "<text x=\"212.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "BFC\n",
       " </text>\n",
       "\n",
       "<rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n",
       "<circle cx=\"272.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" />\n",
       "<path d=\"M 260.0 50.4 A 12.8 12.8 0 0 1 285.6 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n",
       "<path d=\"M 276.9018483174859 33.93539030917347 L 285.21569219381655 29.135390309173467 L 285.21569219381655 38.73539030917347 L 281.8901546432843 36.815390309173466 L 274.0470765814496 50.4 L 272.3843078061835 49.44 L 280.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" />\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f9f7af7c9a0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import display_svg\n",
    "from mindquantum.core.circuit.channel_adder import MixerAdder, MeasureAccepter\n",
    "\n",
    "mixer = MixerAdder([\n",
    "    BitFlipAdder(flip_rate=0.01),\n",
    "    MeasureAccepter(),\n",
    "], add_after=False)\n",
    "print(mixer)\n",
    "\n",
    "circ = Circuit() + G.H(0) + G.RX('a').on(1) + G.Z(1, 0) + G.Measure().on(0)\n",
    "display_svg(circ.svg())\n",
    "new_circ = mixer(circ)\n",
    "new_circ.svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### SequentialAdder\n",
    "\n",
    "```python\n",
    "SequentialAdder(adders: typing.List[ChannelAdderBase])\n",
    "```\n",
    "\n",
    "`SequentialAdder` 是由多个 `Adder` 顺序构成类，量子线路会经过 `SequentialAdder` 中的 `Adder` 依次处理，生成最终的量子线路。例如，我们想构建一个先在测量门前添加一个 $p=0.01$ 的比特翻转信道，然后在 `q1` 比特上的非测量门和非噪声信道后添加 $p=0.05$ 的比特翻转信道。\n",
    "\n",
    "我们首先自定义在某个特比特添加比特翻转信道的 `Adder`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SequentialAdder<\n",
      "  MixerAdder<\n",
      "    MeasureAccepter<>\n",
      "    BitFlipAdder<flip_rate=0.01, with_ctrl=True>\n",
      "  >\n",
      "  CustomBitFlipAdder<q=1, flip_rate=0.05>\n",
      ">\n"
     ]
    }
   ],
   "source": [
    "from mindquantum.core.circuit.channel_adder import ChannelAdderBase, SequentialAdder\n",
    "class CustomBitFlipAdder(ChannelAdderBase):\n",
    "    def __init__(self, q, flip_rate):\n",
    "        self.q = q\n",
    "        self.flip_rate = flip_rate\n",
    "        super().__init__()\n",
    "\n",
    "    def _accepter(self):\n",
    "        return [lambda x: self.q in x.obj_qubits or self.q in x.ctrl_qubits]\n",
    "\n",
    "    def _excluder(self):\n",
    "        return  [lambda x: isinstance(x, (G.Measure, G.NoiseGate))]\n",
    "\n",
    "    def _handler(self, g):\n",
    "        return Circuit([G.BitFlipChannel(self.flip_rate).on(self.q)])\n",
    "\n",
    "    def __repr__(self):\n",
    "        return f\"CustomBitFlipAdder<q={self.q}, flip_rate={self.flip_rate}>\"\n",
    "\n",
    "seq_adder = SequentialAdder([\n",
    "    MixerAdder([\n",
    "        MeasureAccepter(),\n",
    "        BitFlipAdder(flip_rate=0.01),\n",
    "    ], add_after=False),\n",
    "    CustomBitFlipAdder(q=1, flip_rate=0.05),\n",
    "])\n",
    "print(seq_adder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"276.8\" height=\"140.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RX\n",
       " </text>\n",
       "<text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "a\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "<rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n",
       "<circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" />\n",
       "<path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n",
       "<path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" />\n",
       "</svg></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"396.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"396.8\" height=\"140.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"376.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"376.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RX\n",
       " </text>\n",
       "<text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "a\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "BFC\n",
       " </text>\n",
       "\n",
       "<circle cx=\"212.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" />\n",
       "<text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "BFC\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"252.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" />\n",
       "<text x=\"272.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "BFC\n",
       " </text>\n",
       "\n",
       "<rect x=\"312.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n",
       "<circle cx=\"332.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" />\n",
       "<path d=\"M 320.0 50.4 A 12.8 12.8 0 0 1 345.6 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n",
       "<path d=\"M 336.9018483174859 33.93539030917347 L 345.21569219381655 29.135390309173467 L 345.21569219381655 38.73539030917347 L 341.8901546432843 36.815390309173466 L 334.04707658144963 50.4 L 332.3843078061835 49.44 L 340.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" />\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f9ea77e7970>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ = Circuit() + G.H(0) + G.RX('a').on(1) + G.Z(1, 0) + G.Measure().on(0)\n",
    "display_svg(circ.svg())\n",
    "new_circ = seq_adder(circ)\n",
    "new_circ.svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基于 `ChannelAdder` 的噪声模拟器\n",
    "\n",
    "我们可以将如上定义的各种 `Adder` 与现有的模拟器组合，构成一个含噪声模拟器。更进一步，我们可以根据定义的芯片参数与拓扑图，生成一个针对该芯片的噪声模拟器。第二种正在开发中，这里先介绍第一种。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"276.8\" height=\"140.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RX\n",
       " </text>\n",
       "<text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "<rect x=\"192.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n",
       "<circle cx=\"212.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" />\n",
       "<path d=\"M 200.0 110.4 A 12.8 12.8 0 0 1 225.60000000000002 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n",
       "<path d=\"M 216.90184831748593 93.93539030917347 L 225.21569219381655 89.13539030917347 L 225.21569219381655 98.73539030917347 L 221.8901546432843 96.81539030917347 L 214.04707658144957 110.4 L 212.38430780618347 109.44 L 220.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" />\n",
       "</svg></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"401.2\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"401.2\" height=\"147.0\" fill=\"#ffffff\" />\n",
       "<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\n",
       "Shots:\n",
       " 10000\n",
       " </text>\n",
       "<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\n",
       "Keys: q1\n",
       " </text>\n",
       "<line x1=\"31.2\" x2=\"391.2\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"31.2\" x2=\"31.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<text x=\"33.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n",
       "0.0\n",
       " </text>\n",
       "<line x1=\"31.2\" x2=\"31.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"91.2\" x2=\"91.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<text x=\"93.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n",
       "0.155\n",
       " </text>\n",
       "<line x1=\"91.2\" x2=\"91.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n",
       "<line x1=\"151.2\" x2=\"151.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<text x=\"153.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n",
       "0.31\n",
       " </text>\n",
       "<line x1=\"151.2\" x2=\"151.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n",
       "<line x1=\"211.2\" x2=\"211.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<text x=\"213.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n",
       "0.465\n",
       " </text>\n",
       "<line x1=\"211.2\" x2=\"211.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n",
       "<line x1=\"271.2\" x2=\"271.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<text x=\"273.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n",
       "0.62\n",
       " </text>\n",
       "<line x1=\"271.2\" x2=\"271.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n",
       "<line x1=\"331.2\" x2=\"331.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<text x=\"333.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n",
       "0.774\n",
       " </text>\n",
       "<line x1=\"331.2\" x2=\"331.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n",
       "<text x=\"22.2\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n",
       "0\n",
       " </text>\n",
       "<line x1=\"24.2\" x2=\"31.2\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<rect x=\"31.2\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1685600816670417599\" fill=\"#5e7ce0\" />\n",
       "<text x=\"341.2\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1685600816670448489\" fill-opacity=\"0\" >\n",
       "7745\n",
       " </text>\n",
       "<text x=\"22.2\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n",
       "1\n",
       " </text>\n",
       "<line x1=\"24.2\" x2=\"31.2\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<rect x=\"31.2\" y=\"103.0\" width=\"87.34667527437057\" height=\"24\" id=\"bar_1_1685600816670490280\" fill=\"#16acff\" />\n",
       "<text x=\"128.54667527437056\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1685600816670500423\" fill-opacity=\"0\" >\n",
       "2255\n",
       " </text>\n",
       "<animate xlink:href=\"#bar_0_1685600816670417599\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n",
       "<animate xlink:href=\"#bar_1_1685600816670490280\" attributeName=\"width\" from=\"0\" to=\"87.34667527437057\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 87.34667527437057\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n",
       "<animate xlink:href=\"#bar_0_1685600816670417599\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n",
       "<animate xlink:href=\"#bar_text_0_1685600816670448489\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n",
       "<animate xlink:href=\"#bar_text_1_1685600816670500423\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n",
       "<text x=\"203.1\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\n",
       "probability\n",
       " </text>\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f9ea5711f40>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"401.2\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"401.2\" height=\"147.0\" fill=\"#ffffff\" />\n",
       "<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\n",
       "Shots:\n",
       " 10000\n",
       " </text>\n",
       "<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\n",
       "Keys: q1\n",
       " </text>\n",
       "<line x1=\"31.2\" x2=\"391.2\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"31.2\" x2=\"31.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<text x=\"33.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n",
       "0.0\n",
       " </text>\n",
       "<line x1=\"31.2\" x2=\"31.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"91.2\" x2=\"91.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<text x=\"93.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n",
       "0.142\n",
       " </text>\n",
       "<line x1=\"91.2\" x2=\"91.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n",
       "<line x1=\"151.2\" x2=\"151.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<text x=\"153.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n",
       "0.284\n",
       " </text>\n",
       "<line x1=\"151.2\" x2=\"151.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n",
       "<line x1=\"211.2\" x2=\"211.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<text x=\"213.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n",
       "0.427\n",
       " </text>\n",
       "<line x1=\"211.2\" x2=\"211.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n",
       "<line x1=\"271.2\" x2=\"271.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<text x=\"273.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n",
       "0.569\n",
       " </text>\n",
       "<line x1=\"271.2\" x2=\"271.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n",
       "<line x1=\"331.2\" x2=\"331.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<text x=\"333.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n",
       "0.711\n",
       " </text>\n",
       "<line x1=\"331.2\" x2=\"331.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n",
       "<text x=\"22.2\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n",
       "0\n",
       " </text>\n",
       "<line x1=\"24.2\" x2=\"31.2\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<rect x=\"31.2\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1685600816707049769\" fill=\"#5e7ce0\" />\n",
       "<text x=\"341.2\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1685600816707083985\" fill-opacity=\"0\" >\n",
       "7112\n",
       " </text>\n",
       "<text x=\"22.2\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n",
       "1\n",
       " </text>\n",
       "<line x1=\"24.2\" x2=\"31.2\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<rect x=\"31.2\" y=\"103.0\" width=\"121.822272215973\" height=\"24\" id=\"bar_1_1685600816707108163\" fill=\"#16acff\" />\n",
       "<text x=\"163.02227221597298\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1685600816707123386\" fill-opacity=\"0\" >\n",
       "2888\n",
       " </text>\n",
       "<animate xlink:href=\"#bar_0_1685600816707049769\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n",
       "<animate xlink:href=\"#bar_1_1685600816707108163\" attributeName=\"width\" from=\"0\" to=\"121.822272215973\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 121.822272215973\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n",
       "<animate xlink:href=\"#bar_0_1685600816707049769\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n",
       "<animate xlink:href=\"#bar_text_0_1685600816707083985\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n",
       "<animate xlink:href=\"#bar_text_1_1685600816707123386\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n",
       "<text x=\"203.1\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\n",
       "probability\n",
       " </text>\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f9ea5723280>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"456.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"456.8\" height=\"140.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"436.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"436.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RX\n",
       " </text>\n",
       "<text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "BFC\n",
       " </text>\n",
       "\n",
       "<circle cx=\"212.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" />\n",
       "<text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "BFC\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"312.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" />\n",
       "<text x=\"332.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "BFC\n",
       " </text>\n",
       "\n",
       "<rect x=\"372.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n",
       "<circle cx=\"392.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" />\n",
       "<path d=\"M 380.0 110.4 A 12.8 12.8 0 0 1 405.6 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n",
       "<path d=\"M 396.9018483174859 93.93539030917347 L 405.21569219381655 89.13539030917347 L 405.21569219381655 98.73539030917347 L 401.8901546432843 96.81539030917347 L 394.04707658144963 110.4 L 392.3843078061835 109.44 L 400.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" />\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f9ea5723a30>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from mindquantum.simulator import Simulator\n",
    "from mindquantum.simulator.noise import NoiseBackend\n",
    "\n",
    "noiseless_sim = Simulator('mqvector', 2)\n",
    "noiseless_circ = Circuit().h(0).rx(1.0, 1).z(1, 0).measure(1)\n",
    "display_svg(noiseless_circ.svg())\n",
    "res1 = noiseless_sim.sampling(noiseless_circ, shots=10000)\n",
    "display(res1.svg())\n",
    "\n",
    "\n",
    "\n",
    "noise_sim = Simulator(NoiseBackend('mqvector', 2, seq_adder))\n",
    "res2 = noise_sim.sampling(noiseless_circ, shots=10000)\n",
    "display(res2.svg())\n",
    "display(noise_sim.backend.transform_circ(noiseless_circ).svg())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
